/**
 * 用户服务
 * @TransactionAnnotation表示开启事务
 */
import TransactionAnnotation from "../../package/TransactionAnnotation";

export default class PermissionService {
    /**
     * 注册用户
     * @param table                 指定表名称3
     * @param user                  用户属性
     * @returns {Promise<*>|void}
     *
     */

    addPermissionEntity(entity) {
        return this['session'].save('t_permission', entity);
    }

    /**
     * 保存（数据库创建了外键，不需要程序来开启事务删除）
     * @param table
     * @param userName
     * @returns {*}
     */
    //@TransactionAnnotation
    removePermissionEntity(entity) {
        // return new Promise(async (resolve, reject) => {
        //
        //     const a = await this['session'].remove('t_permission', entity);
        //     const sql = 'delete from t_role_permission where t_pid = ?';
        //     const b = await this['session'].executeSQL(sql, [entity.t_id]);
        //     resolve([a, b]);
        // })
        return this['session'].remove('t_permission', entity);
    }

    /**
     * 更新
     * @param table
     * @param entity
     * @returns {*}
     */
    updatePermissionEntity(entity) {
        return this['session'].update('t_permission', entity);
    }

    /**
     * 查询
     * @param table
     * @param start
     * @param size
     * @returns {*}
     */
    listPermissionEntities(start, size, keyWord) {
        return new Promise(async (resolve, reject) => {
            try {
                const where = keyWord ? "WHERE `t_permName` LIKE '%" + keyWord + "%' " : "";
                const sql = 'SELECT c.* ,t_dictionary_detail.`t_info` FROM (SELECT a.* FROM t_permission a INNER JOIN (SELECT t_id FROM t_permission  ' + where + ' ORDER BY t_createAt DESC LIMIT 0,20 ) b ON a.t_id = b.t_id) AS c LEFT JOIN t_dictionary_detail  ON  c.`t_classify`= t_dictionary_detail.`t_id`'
                const rows = await this['session'].exeSQL(sql, [start, size]);
                const count = await this['session'].exeSQL("select count(*)  from t_permission " + where, []);
                resolve({total: count[0]['count(*)'], records: rows});
            } catch (e) {
                reject(e)
            }
        })

    }


}


